package top.yxf.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.yxf.gulimall.product.dao.BrandDao;
import top.yxf.gulimall.product.dao.CategoryBrandRelationDao;
import top.yxf.gulimall.product.dao.CategoryDao;
import top.yxf.gulimall.product.entity.BrandEntity;
import top.yxf.gulimall.product.entity.CategoryBrandRelationEntity;
import top.yxf.gulimall.product.entity.CategoryEntity;
import top.yxf.gulimall.product.service.BrandService;
import top.yxf.gulimall.product.service.CategoryBrandRelationService;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName CategoryBrandRelationServiceImpl
 * @Description TODO
 * @Author zw
 * @Date 2021/10/27 下午10:17
 * @Version 1.0
 */
@Service
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Resource
    private BrandDao brandDao;
    @Resource
    private CategoryDao categoryDao;

    @Resource
    private CategoryBrandRelationDao relationDao;

    @Resource
    private BrandService brandService;

    @Override
    public List<CategoryBrandRelationEntity> list(Long brandId) {
        QueryWrapper<CategoryBrandRelationEntity> queryWrapper = new QueryWrapper<>();
        if (brandId != null) {
            queryWrapper.eq("brand_id", brandId);
        }
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelationEntity) {
        Long brandId = categoryBrandRelationEntity.getBrandId();
        Long categoryId = categoryBrandRelationEntity.getCatelogId();
        BrandEntity brandEntity = brandDao.selectById(brandId);
        if (null != brandEntity) {
            categoryBrandRelationEntity.setBrandName(brandEntity.getName());
        }
        CategoryEntity categoryEntity = categoryDao.selectById(categoryId);

        if (null != categoryEntity) {
            categoryBrandRelationEntity.setCatelogName(categoryEntity.getName());
        }

        try {
            this.save(categoryBrandRelationEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateBrand(Long brandId, String name) {
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setBrandName(name);
        categoryBrandRelationEntity.setBrandId(brandId);
        this.update(categoryBrandRelationEntity, new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));
    }

    @Transactional
    @Override
    public void updateCategory(Long catId, String name) {
        this.baseMapper.updateCategory(catId, name);
    }

    @Override
    public List<BrandEntity> getBrandsByCatId(Long catId) {
        List<CategoryBrandRelationEntity> categoryBrandRelationEntityList = relationDao.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
        List<BrandEntity> brandEntities = categoryBrandRelationEntityList.stream().map(categoryBrandRelationEntity -> {
            return brandService.getById(categoryBrandRelationEntity.getBrandId());
        }).collect(Collectors.toList());
        return brandEntities;
    }
}
